// 震动功能调试工具
class VibrateDebug {
  constructor() {
    this.logs = [];
  }

  log(message, type = 'info') {
    const timestamp = new Date().toLocaleTimeString();
    const logEntry = { message, type, timestamp };
    this.logs.push(logEntry);
    console.log(`[${timestamp}] ${type.toUpperCase()}: ${message}`);
    return logEntry;
  }

  // 全面的设备信息检测
  async detectDeviceInfo() {
    this.log('=== 设备信息检测 ===', 'info');
    
    // #ifdef APP-PLUS
    try {
      const deviceInfo = uni.getSystemInfoSync();
      this.log(`设备型号: ${deviceInfo.model}`, 'info');
      this.log(`系统版本: ${deviceInfo.system}`, 'info');
      this.log(`平台: ${deviceInfo.platform}`, 'info');

      if (deviceInfo.platform === 'android') {
        const main = plus.android.runtimeMainActivity();
        const Build = plus.android.importClass('android.os.Build');
        
        this.log(`Android版本: ${Build.VERSION.RELEASE}`, 'info');
        this.log(`API级别: ${Build.VERSION.SDK_INT}`, 'info');
        this.log(`设备制造商: ${Build.MANUFACTURER}`, 'info');
        
        // 检查震动服务
        const Context = plus.android.importClass('android.content.Context');
        const vibrator = main.getSystemService(Context.VIBRATOR_SERVICE);
        
        if (vibrator && vibrator.hasVibrator()) {
          this.log('✅ 设备支持震动', 'success');
        } else {
          this.log('❌ 设备不支持震动', 'error');
        }
      }
    } catch (error) {
      this.log(`设备信息检测失败: ${error.message}`, 'error');
    }
    // #endif
  }

  // 权限检测
  async checkPermissions() {
    this.log('=== 权限检测 ===', 'info');
    
    // #ifdef APP-PLUS
    try {
      const main = plus.android.runtimeMainActivity();
      const PackageManager = plus.android.importClass('android.content.pm.PackageManager');
      
      const vibratePermission = 'android.permission.VIBRATE';
      const result = main.checkSelfPermission(vibratePermission);
      const hasPermission = result === PackageManager.PERMISSION_GRANTED;
      
      this.log(`震动权限: ${hasPermission ? '已授予' : '未授予'}`, 
               hasPermission ? 'success' : 'error');
      return hasPermission;
    } catch (error) {
      this.log(`权限检测失败: ${error.message}`, 'error');
      return false;
    }
    // #endif
    return true;
  }

  // 获取日志
  getLogs() {
    return this.logs;
  }

  // 清空日志
  clearLogs() {
    this.logs = [];
  }
}

export default new VibrateDebug();